// pages/student/index/home/index.js
import {getBanners} from "../../../../service/Student";
import {getAlumniList, getEntryCompanyList} from "../../../../service/Personnel";
import Config from "../../../../main.config";
import {getStorageSync} from "../../../../utils/Storage";
import {getList, getMajorList} from "../../../../service/Education";
import {getPublishPositionList} from "../../../../service/Position";
Component({
    /*
    * 默认接收全局样式
    * */
    options: {
        addGlobalClass: true,
        styleIsolation: "shared"
    },

    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 页面的初始数据
     */
    data: {
        //banner图
        banners: [],
        //tabs菜单
        _tabs: {
            active: 0,
            list: [{name: "全职"}, {name: "兼职"}]
        },
        /*筛选菜单*/
        _position: { //职位
            list: [
                {text: "全部职位", value: 0}
            ],
            current: 0
        },
        _major: { //专业
            list: [
                {text: "全部专业", value: 0}
            ],
            current: 0
        },
        _company: { //公司
            list: [
                {text: "选择公司", value: 0}
            ],
            current: 0
        },
        _educationAction: {
            show: false,
            actions: []
        },
        //招聘信息
        _senior: {
            loading: false,
            reset: false,
            error: false,
            total: -1,
            list: []
        },
    },

    lifetimes: {
        attached: function () {
            let registerInfo = getStorageSync(Config.storage.registerInfo) || {};
            this.data._registerInfo = registerInfo;
            this.getBanners();
            this.getCompanyList();
            this.getEducationList();
            this.data._search = {nature: "全职", schoolNo: registerInfo.schoolNo};
            this.getMajorList();
            this.getPositionTypes();
            this.data._schoolName = registerInfo.schoolName;
            this.setData({"_senior.loading": true});
            wx.setNavigationBarTitle({title: "校友(" + registerInfo.schoolName + ")"})
        }
    },

    pageLifetimes: {
        show: function () {
            let schoolName = this.data._schoolName;
            wx.setNavigationBarTitle({title: "校友(" + schoolName + ")"})
        },
    },

    /**
     * 组件的方法列表
     */
    methods: {

        /*
        * 获取banners
        * */
        getBanners: async function () {
            try {
                let res = await getBanners();
                this.setData({banners: res})
            } catch (e) {
                console.error(e)
            }
        },
        /*
        * 跳转详情
        * */
        bindBannerNavigator: function (e) {
            let banners = this.data.banners;
            let index = e.currentTarget.dataset.index;
            let currentData = banners[index];
            console.log(currentData)
            if (currentData.type === 1) {
                wx.navigateTo({url: currentData.toUrl})
            } else {
                wx.navigateTo({url: "/pages/common/web/index?url=" + currentData.toUrl})
            }
        },

        /*
        * tabs切换
        * */
        onTabsChange: function (e) {
            let active = this.data._tabs.active;
            let index = e.detail.index;
            let search = this.data._search;
            let title = e.detail.title;
            if (active === index) return false;
            search.nature = title;
            this.setData({
                "_tabs.active": index,
                "_senior.reset": true
            })
        },


        /*
        * 获取职位类型
        * */
        getPositionTypes: async function () {
            try {
                let schoolNo = this.data._search.schoolNo || "";
                let res = await getPublishPositionList({schoolNo});
                let list = [{text: "全部职位", value: 0}];
                if (res.length === 0) return false;
                let newList = res.map(function (item, index) {
                    return {
                        text: item.name,
                        value: index + 1,
                        search: item.name
                    }
                })
                this.setData({
                    "_position.list": list.concat(newList)
                })
            } catch (e) {
                console.error(e)
            }
        },

        /*
        * 获取专业列表
        * */
        getMajorList: async function () {
            try {
                let schoolNo = this.data._search.schoolNo || "";
                let res = await getMajorList(schoolNo);
                let list = [{text: "全部专业", value: 0 }];
                if (res.length === 0) return false;
                let newList = res.map(function (item, index) {
                    return {
                        text: item.professionalName,
                        value: index + 1,
                        no: item.professionalNo
                    }
                })
                this.setData({
                    "_major.list": list.concat(newList)
                })
            } catch (e) {
                console.error(e)
            }
        },

        /*
        * 获取学长所在公司
        * */
        getCompanyList: async function () {
            try {
                let schoolNo = this.data._registerInfo.schoolNo;
                if (!schoolNo) {return false}
                let res = await getEntryCompanyList(schoolNo);
                let list = this.data._company.list;
                if (res.length === 0) return false;
                let newList = res.map(function (item, index) {
                    return {
                        text: item.companyName,
                        value: index + 1,
                        companyId: item.companyId
                    }
                })
                this.setData({
                    "_company.list": list.concat(newList)
                })
            } catch (e) {
                console.error(e)
            }
        },

        /*
        * 获取受教育列表
        * */
        getEducationList: async function () {
            try {
                let userId = this.data._registerInfo.createUser;
                let list = await await getList(userId);
                list.length > 0 ? this.setData({
                    "_educationAction.actions": list.map(function (item) {
                        return {
                            name: item.schoolName,
                            schoolNo: item.schoolNo
                        }
                    })
                }) : null
            } catch (e) {
                console.error(e)
            }
        },

        /*
        * 列表加载
        * */
        onLoadList: async function (e) {
            try {
                let search = this.data._search;
                let paging = e.detail.paging;
                let list = this.data._senior.list;
                let isReset = this.data._senior.reset;
                let postData = Object.assign(search, {
                    page: paging.page,
                    limit: paging.pageSize,
                })
                let res = await getAlumniList(postData);
                let newList = res.data.map(function (item) {
                    let gradeArr = item.grade.split(";");
                    let grade = gradeArr[1] || gradeArr[0];
                    let logo = Config.api.downLoad + "?fileId=" + item.logo
                    return {
                        id: item.id,
                        logo,
                        name: item.name,
                        companyName: item.companyName,
                        position: item.position,
                        schoolName: item.schoolName,
                        professionalName: item.professionalName,
                        grade: grade.substr(0, 4),
                        positionList: item.positionList.map(function (cItem) {
                            return cItem.name
                        }),
                        createUser: item.createUser
                    }
                });
                list = isReset ? newList : list.concat(newList);
                this.setData({
                    "_senior.list": list,
                    "_senior.total": res.count
                });
            } catch (e) {
                console.error(e)
                this.setData({
                    "_senior.error": true
                })
            } finally {
                this.setData({
                    "_senior.reset": false,
                    "_senior.loading": false
                })
            }
        },

        /*
        * 监听menu切换
        * */
        onMenuChange: function (e) {
            let name = e.currentTarget.dataset.name;
            let index = e.detail;
            let current = this.data[name].current;
            if (index === current) return false;
            let search = this.data._search;
            let list = this.data[name].list;
            let currentData = list[index];
            switch (name) {
                case "_position":
                    index > 0 ? search.position = currentData.text : delete search.position;
                    break
                case "_major":
                    currentData.no ? search.professionalNo = currentData.no : delete search.professionalNo
                    break
                case "_company":
                    index > 0 ? search.companyId = currentData.companyId : delete search.companyId
                    break
                default:
                    break
            }
            this.setData({
                [name + ".current"]: index,
                "_senior.reset": true
            })
        },

        /*
        * 切换学校
        * */
        bindSchoolChange: function () {
            this.setData({"_educationAction.show": true})
        },
        onActionClose: function () {
            this.setData({"_educationAction.show": false})
        },
        onActionSelect: function (e) {
            let search = this.data._search;
            let {name, schoolNo} = e.detail
            if (search.schoolNo !== schoolNo) {
                this.data._search.schoolNo = schoolNo;
                this.data._schoolName = name;
                this.getMajorList();
                this.getPositionTypes();
                this.setData({"_senior.reset": true});
                wx.setNavigationBarTitle({title: "校友(" + name + ")"})
            }
        },
        /*
        * 查看详情
        * */
        bindNavigatorToDetail: function (e) {
            let id = e.currentTarget.dataset.id;
            wx.navigateTo({
                url: "/packageStudent/pages/detail/index?id=" + id
            })
        }
    }
})